package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.Letter;
import cn.wolfcode.trip.base.domain.TravelConcern;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/users")
@Controller
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelConcernService travelConcernService;
    @Autowired
    private ITravelConcernService concernService;
    @Autowired
    private ILetterService letterService;
    @Autowired
    private ISystemService systemService;
    @Autowired
    private ICommentService commentService;

    //获取用户未读取的评论
    @GetMapping("/{AuthorNickName}/noCommentMe")
    @ResponseBody
    public Object selectNoCommentMe(@PathVariable String AuthorNickName){
        List<Comment> list = commentService.selectNoCommentMe(AuthorNickName);
        return list;
    }

    //当用户查看未读取的评论时状态为1
    @PutMapping("/updateCommentMe")
    @ResponseBody
    public Object updateCommentMe(String AuthorNickName,Long[] ids){
        commentService.updateCommentMe(AuthorNickName,ids);
        return new JSONResult();
    }

    //通过当前用户查询对其进行评论
    @GetMapping("/{AuthorNickName}/commentMe")
    @ResponseBody
    public Object selectCommentMe(@PathVariable String AuthorNickName){
        List<Comment> list = commentService.selectCommentMe(AuthorNickName);
        return list;
    }

    //获取当前用户游记的评论
    @GetMapping("/{userId}/travelComment")
    @ResponseBody
    public Object getUserTravelComment(@PathVariable Long userId){
        List<Comment> list = commentService.getUserTravelComment(userId);
        return list;
    }


    //获取当前用户已发布的游记篇数
    @GetMapping("/{userId}/travelNums")
    @ResponseBody
    public Object getTravelNums(@PathVariable Long userId){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("travelNums", userService.getTravelNums(userId));
        return map;
    }



    @GetMapping("/{userId}/travels/{travelId}/concernCollector")
    @ResponseBody
    //判断当前登录用户是否已经对某游记收藏
    public Object checkUserConcernCollector(@PathVariable Long userId,@PathVariable Long travelId){
        JSONResult result = new JSONResult();
        //如果点评对象存在表示用户已经对游记进行收藏
        try {
            travelConcernService.checkUserConcernCollector(userId, travelId);
        }catch (Exception e){
            System.out.println(e.getMessage());
            result.mark(e.getMessage());
        }
        return result;
    }


    @GetMapping("/{userId}/travels/{travelId}/travelConcerns")
    @ResponseBody
    //判断当前登录用户是否已经对某游记点赞
    public Object checkUserTravelConcerns(@PathVariable Long userId,@PathVariable Long travelId){
        JSONResult result = new JSONResult();
        //如果点评对象存在表示用户已经对游记进行点评
        try {
            travelConcernService.checkUserTravelConcerns(userId, travelId);
        }catch (Exception e){
            System.out.println(e.getMessage());
            result.mark(e.getMessage());
        }
        return result;
    }



    @GetMapping("/{userId}/travels")
    @ResponseBody
    //我的游记查询当前用户的所有游记
    //游记这里存在问题之后修改
    public Object myTravel(TravelQuery qo) {
        //我的游记按照最后更新时间进行倒序
        qo.setPageSize(0);
        qo.setOrderby("t.lastUpdateTime desc");
        PageInfo pageInfo = travelService.getPageInfo(qo);
        return pageInfo;
    }

    @PutMapping
    @ResponseBody
    //APP个人资料的
    public Object update(User user) {
        JSONResult result = new JSONResult();
        try {
            userService.userUpdate(user);
            //成功更新之后 需要把更新后的数据返回给静态页面 用于静态页面上更新后数据的回显
            User temp = userService.get(user.getId());
            result.setData(temp);
            //SESSION中的当前用户数据也一并进行更新
            UserContext.setCurrentUser(temp);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        return result;
    }

    //用户注销操作
    @RequestMapping(value = "/session", method = RequestMethod.DELETE)
    @ResponseBody
    public Object loginout(HttpSession session) {
        //把当前用户的session信息销毁
        session.invalidate();
        return new JSONResult();
    }

    //用户登录接口
    @RequestMapping(value = "/session", method = RequestMethod.POST)
    @ResponseBody
    public Object login(User user) {
        JSONResult result = new JSONResult();
        try {
            User loginUser = userService.login(user);
            //如果登录成功把当前用户设置进Session中
            UserContext.setCurrentUser(loginUser);
            //登录成功后把当前登录的用户数据设置进JSONResult 用于给静态页面进行数据获取
            result.setData(loginUser);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        return result;
    }

    //该接口用户测试远程邮箱验证
    @RequestMapping(value = "/checkEmail", method = RequestMethod.GET)
    @ResponseBody
    public boolean test5(String email) {
        System.out.println("邮箱校验");
        //邮箱查重复
        User user = userService.checkEmail(email);
        return user == null;
    }

    /*@ApiOperation(value = "注册功能",notes = "其实就是新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "昵称",name = "nickName",dataType = "String",required = true),
            @ApiImplicitParam(value = "邮箱",name = "email",dataType = "String",required = true),
            @ApiImplicitParam(value = "密码",name = "password",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="用户注册成功")
    })*/

    //用户注册操作方法
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public Object register(User user) {
        int num;
        JSONResult result = new JSONResult();
        try {
            userService.userRegister(user);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        //把注册结果返回回去
        return result;
    }

    //获取用户的游记的点赞
    @ResponseBody
    @GetMapping("/{userId}/travels/travelconcerns")
    public Object travelLike(TravelConcernQueryObject qo) {
        qo.setOrderBy("tc.likeDate DESC");

        PageInfo<TravelConcern> query = concernService.query(qo);
        concernService.updateIsViewed(qo.getTravelId());
        return query;
    }

    //获取未查看的点赞
    @ResponseBody
    @GetMapping("/{userId}/travelconcerns")
    public Object notLike(@PathVariable Long userId) {
        int count = concernService.selectNotLike(userId);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("count", count);
        return map;
    }
        
    //查询id的人
    @ResponseBody
    @GetMapping("/{userId}")
    public Object selectByUserId(@PathVariable Long userId) {
        return userService.get(userId);
    }

    //将私信发送给某人
    @ResponseBody
    @GetMapping("/{messages.id}/{dMessages.id}/{content}/letters")
    public Object insertByUserId(Letter letter) {
        JSONResult result = new JSONResult();
        try {
            letterService.insertByUserId(letter);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //未读私信信息条数
    @ResponseBody
    @GetMapping("/{userId}/letters")
    public Object notLetters(@PathVariable Long userId) {
        int count = letterService.notletters(userId);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("count", count);
        return map;
    }
    //获取用户私信
    @ResponseBody
    @GetMapping("/{userId}/letters/content")
    public Object getLetters(LetterQueryObject qo) {
        qo.setOrderBy("l.messagesDate desc");
        letterService.updateIsViewed(qo.getUserId());
        return letterService.query(qo);
    }
    //未读系统信息条数
    @ResponseBody
    @GetMapping("/{userId}/systems")
    public Object notSystems(@PathVariable Long userId) {
        int count = systemService.notSystems(userId);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("count", count);
        return map;
    }
    //获取系统信息
    @ResponseBody
    @GetMapping("/{userId}/travels/systemMsgs")
    public Object getSystems(SystemMsgQueryObject qo) {
        systemService.updateIsViewed(qo.getUserId());
        return systemService.query(qo);
    }

    //获取当前用户的朋友
    @GetMapping("/{id}/friends")
    @ResponseBody
    public List queryFriends(@PathVariable Long id){
        return userService.queryFriends(id);
    }

    //获取多个用户
    @GetMapping("/more")
    @ResponseBody
    public List queryManyUsers(Long[] ids){
        return userService.queryManyUsers(ids);
    }

    //获取当前用户朋友圈列表
    @GetMapping("/{id}/friendNews")
    @ResponseBody
    public Object queryFriendNews(@PathVariable Long id,FriendNewsQueryObject qo){
        Long currentUserId = UserContext.getCurrentUser().getId();
        return userService.queryFriendNews(id,qo,currentUserId);
    }
    @GetMapping("/getFriends")
    public Object getFridends() {
        User user = UserContext.getCurrentUser();
        List<Map> info = userService.listAllFridends(user.getId());
        return info;
    }

}
